package cn.cs.yangtai.service.impl;

import cn.cs.yangtai.entity.Blog;
import cn.cs.yangtai.entity.vo.BlogVo;
import cn.cs.yangtai.entity.vo.PageResult;
import cn.cs.yangtai.mapper.BlogMapper;
import cn.cs.yangtai.service.BlogService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Blog)表服务实现类
 *
 * @author makejava
 * @since 2024-12-12 09:23:23
 */
@Service("blogService")
public class BlogServiceImpl implements BlogService {
    @Resource
    private BlogMapper blogMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param blogId 主键
     * @return 实例对象
     */
    @Override
    public BlogVo queryById(Integer blogId) {
        return this.blogMapper.queryById(blogId);
    }

    @Override
    public List<BlogVo> queryByTitle(String title) {
        return blogMapper.queryByTitle(title);
    }
    /**
     * 通过ID查询单条数据
     *
     * @param blogId 主键
     * @return 实例对象
     */
    @Override
    public Blog queryBy(Integer blogId) {
        return this.blogMapper.queryBy(blogId);
    }

    /**
     * 分页查询
     *
     * @param cuttentPage 本页
     * @param pageSize    分页大小
     * @return 查询结果
     */
    @Override
    public PageResult<BlogVo> queryByPage(Integer cuttentPage, Integer pageSize) {
        PageHelper.startPage(cuttentPage,pageSize);
        Page<BlogVo> blogs =blogMapper.queryAllByLimit(new Page<>());
        return PageResult.restPage(blogs);
    }

    @Override
    public List<BlogVo> queryByUserid(Integer userid) {
        return blogMapper.queryByUserid(userid);
    }

    /**
     * 新增数据
     *
     * @param blog 实例对象
     * @return 实例对象
     */
    @Override
    public Blog insert(Blog blog) {
        this.blogMapper.insert(blog);
        return blog;
    }

    /**
     * 修改数据
     *
     * @param blog 实例对象
     * @return 实例对象
     */
    @Override
    public BlogVo update(Blog blog) {
        this.blogMapper.update(blog);
        return this.queryById(blog.getBlogId());
    }

    /**
     * 通过主键删除数据
     *
     * @param blogId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer blogId) {
        return this.blogMapper.deleteById(blogId) > 0;
    }
}
